package com.docmgmt.mvp.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {

    public static final String RAGFLOW_SYNC_QUEUE = "ragflow.sync.queue";
    public static final String RAGFLOW_SYNC_EXCHANGE = "ragflow.sync.exchange";
    public static final String RAGFLOW_DLX_QUEUE = "ragflow.dlx.queue";
    public static final String RAGFLOW_DLX_EXCHANGE = "ragflow.dlx.exchange";

    @Bean
    public Queue ragflowSyncQueue() {
        return QueueBuilder.durable(RAGFLOW_SYNC_QUEUE)
            .withArgument("x-dead-letter-exchange", RAGFLOW_DLX_EXCHANGE)
            .withArgument("x-dead-letter-routing-key", "ragflow.dlx")
            .withArgument("x-message-ttl", 3600000)
            .build();
    }

    @Bean
    public TopicExchange ragflowSyncExchange() {
        return new TopicExchange(RAGFLOW_SYNC_EXCHANGE);
    }

    @Bean
    public Binding binding(Queue ragflowSyncQueue, TopicExchange ragflowSyncExchange) {
        return BindingBuilder.bind(ragflowSyncQueue)
            .to(ragflowSyncExchange)
            .with("file.*");
    }

    @Bean
    public Queue deadLetterQueue() {
        return new Queue(RAGFLOW_DLX_QUEUE);
    }

    @Bean
    public DirectExchange deadLetterExchange() {
        return new DirectExchange(RAGFLOW_DLX_EXCHANGE);
    }

    @Bean
    public Binding dlxBinding(Queue deadLetterQueue, DirectExchange deadLetterExchange) {
        return BindingBuilder.bind(deadLetterQueue)
            .to(deadLetterExchange)
            .with("ragflow.dlx");
    }

    @Bean
    public MessageConverter jackson2JsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory, MessageConverter messageConverter) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(messageConverter);
        return rabbitTemplate;
    }

    @Bean
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(
            ConnectionFactory connectionFactory, MessageConverter messageConverter) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setMessageConverter(messageConverter);
        factory.setConcurrentConsumers(3);
        factory.setMaxConcurrentConsumers(10);
        factory.setPrefetchCount(1);
        return factory;
    }
}
